import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect : '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
  },
  {
    path: '/free',
    name: 'Free',
    component: () => import(/* webpackChunkName: "free" */ '../views/Free.vue')
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    redirect: '/user/cart',
    children :[
      {path: 'cart', component: () => import('../components/user/Cart.vue') }
    ]
  },
  {
    path: '/detils',
    name: 'Detils',
    component: () => import(/* webpackChunkName: "detils" */ '../views/Details.vue')
  },
  {
    path: '*',
    name: 'Error',
    component: () => import(/* webpackChunkName: "Error" */ '../views/Error.vue')
  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 解决重复点击路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

// 全局前置守卫
// router.beforeEach((to,from,next) => {
  // console.log('前置守卫',to,from);
  // let mytoken = localStorage.getItem('x-auth-token')
  //   if(to.path == '/user'){
  //     if(mytoken){
  //       next()
  //     }else {
  //       store.dispatch('showToast/asyncIsShowToast',{
  //         show: true,
  //         toastMsg: '请先登录',
  //         toastType: 'danger'
  //       })
  //     }
  //   }
// })
export default router
